package com.caocaog.market.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.caocaog.market.annotation.IgnoreAuth;
import com.caocaog.market.entity.Member;
import com.caocaog.market.entity.MemberPointStatement;
import com.caocaog.market.service.IMemberPointStatementService;
import com.caocaog.market.service.MemberService;
import com.caocaog.market.util.CollectionsUtil;
import com.caocaog.market.vo.MemberPointModifyVO;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Description
 * @Author Van.Planifolia
 * @Date 2024/1/12
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/marketing")
public class ApiMarketingController {
    @Resource
    private MemberService memberService;
    @Resource
    private IMemberPointStatementService memberPointStatementService;

    /**
     * 根据客户手机号查询客户信息
     *
     * @param memberPhone 客户手机号
     * @return 查询到的客户信息
     */
    @IgnoreAuth
    @ApiOperation("查询客户信息根据手机号")
    @GetMapping("/getMemberByPhone/{memberPhone}")
    public Result<?> getMemberByPhone(@PathVariable String memberPhone) {
        Member member = Optional.ofNullable(memberService.getOne(new LambdaQueryWrapper<Member>().eq(Member::getMobile, memberPhone))).orElseThrow(() -> new JeecgBootException(String.format("无法根据手机号 %s 查询到客户信息！", memberPhone)));
        return Result.OK("查询成功！", member);
    }

    /**
     * 积分变动接口
     *
     * @param memberPointModifyVO 变动的VO
     * @return 变动的结果
     */
    @IgnoreAuth
    @ApiOperation("积分变动，增加传正值，减少传负值")
    @PostMapping("/modifyMemberPoint")
    public Result<?> modifyMemberPoint(@Validated @RequestBody MemberPointModifyVO memberPointModifyVO) {
        BigDecimal afterModifyPoint = memberPointStatementService.modifyMemberPoint(memberPointModifyVO);
        return Result.OK(String.format("积分变动成功，变动后数值为 %s", afterModifyPoint.toString()));
    }

    /**
     * 积分查询结果
     *
     * @param memberId 客户Id
     * @return 变动的结果
     */
    @ApiOperation("积分查询，走缓存")
    @GetMapping("/getPointByMemberId")
    public Result<?> getPointByMemberId(@RequestParam String memberId) {
        // 查缓存
        List<MemberPointStatement> memberPointByCache = memberPointStatementService.getMemberPointByCache(memberId);
        // 构建Map
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("sumPoint", new BigDecimal("0.00"));
        resultMap.put("pointStatement", memberPointByCache);
        // 返回
        if (!CollectionsUtil.isEmpty(memberPointByCache)) {
            BigDecimal sumPoint = memberPointByCache.stream().map(MemberPointStatement::getModifyPoint).reduce(BigDecimal.ZERO, BigDecimal::add);
            resultMap.put("sumPoint", sumPoint);
        }
        return Result.OK("查询成功", resultMap);
    }
}
